package com.dingding.service.activity.core.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.Activity;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.dto.activity.GetActivityListDto;
import com.dingding.facde.activity.dto.activity.ReqGetActivityList;
import com.dingding.facde.activity.dto.activity.ResGetActivityList;
import com.dingding.facde.activity.enums.GetActivityListEnum;
import com.dingding.service.activity.core.biz.IActivityListService;
import com.dingding.service.activity.core.dao.IActivityDao;


/**
 * @Description:获取活动列表service
 * @author fengshuonan
 * @date 2016年7月25日 下午4:15:06
 * @version 叮叮搭车
 * @since 2.3.2
 */
@Component("activityListService")
public class ActivityListServiceImpl extends AbstractResponse<ResGetActivityList, ReqGetActivityList> implements IActivityListService{

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource
	private IActivityDao activityDao;
	
	/**
	 * @Description: 获取活动列表
	 * @author fengshuonan
	 * @since 2.3.2
	 */
	@Override
	@Transactional(readOnly = true)
	public String getActivityList(String requestData) {
		
		ResponseEntity<ResGetActivityList> response = new ResponseEntity<ResGetActivityList>();
		
		ReqGetActivityList reqGetActivityList = super.parseRequestJson(requestData);
		
		//验证参数
		if(!super.validateParam(response, reqGetActivityList)){
			return super.responseBeanToJson(response);
		}
		
		//查询数据
		response = queryData(response,reqGetActivityList);
		
		return super.responseBeanToJson(response);
	}
	
	
	/**
	 * @Description:查询活动列表
	 * @author fengshuonan
	 * @since 2.3.2
	 */
	private ResponseEntity<ResGetActivityList> queryData(ResponseEntity<ResGetActivityList> response, ReqGetActivityList reqGetActivityList) {
		
		List<Activity> activityList = null;
		
		try {
			activityList = this.activityDao.getActivityList
					(reqGetActivityList.getUserId(), reqGetActivityList.getPageIndex());
		} catch (Exception e) {
			log.error("查询活动列表出错！");
			response.setCode(GetActivityListEnum.DB_ERROR.getCode());
			response.setErrorMessage(GetActivityListEnum.DB_ERROR.getMessage());
		}
		
		List<GetActivityListDto> dtoList = new ArrayList<GetActivityListDto>();
		for(Activity acti : activityList){
			GetActivityListDto dto = new GetActivityListDto();
			BeanUtils.copyProperties(acti, dto);
			dto.setTimeStamp(DateFormatUtil.getTimeInMillisByTime(dto.getCreateTime()));
			dto.setPropagandaContent(null);
			dto.setPropagandaImage(ToolUtil.imageAddress(acti.getPropagandaImage()));
			dtoList.add(dto);
		}
		
		ResGetActivityList resGetActivityList = new ResGetActivityList();
		resGetActivityList.setActivityList(dtoList);
		response.setCode(GetActivityListEnum.OK.getCode());
		response.setErrorMessage(GetActivityListEnum.OK.getMessage());
		response.setResponseBody(resGetActivityList);
		return response;
	}

}
